<?php

/**
 * This is the model class for table "announcement_view".
 *
 * The followings are the available columns in table 'announcement_view':
 * @property integer $user_id
 * @property integer $announcement_id
 */
class AnnouncementView extends AnnouncementViewBase
{
	public $count_view;
	/**
	 * Returns the static model of the specified AR class.
	 * @return AnnouncementView the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'announcement_view';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, announcement_id', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, announcement_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'member' => array(self::BELONGS_TO, 'Member', 'user_id', 'joinType' => 'INNER JOIN'),
			'announcement' => array(self::BELONGS_TO, 'Announcement', 'announcement_id', 'joinType' => 'INNER JOIN'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'announcement_id' => 'Announcement',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('announcement_id',$this->announcement_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function countByAnnouncement($announcement_id)
	{
		$result = array();
		// Count from assign
		$criteria = new CDbCriteria;
		$criteria->with = array('announcement', 'member', 'member.memberCode');
		$criteria->select = 'announcement_id,
							 COUNT(t.id) as count_view';
		if (!is_array($announcement_id) && $announcement_id != "") {
			$criteria->condition = 't.announcement_id = :announcement_id';
			$criteria->params = array(':announcement_id'=> $announcement_id);
		}
		elseif (is_array($announcement_id) && !empty($announcement_id)) {
			$criteria->addInCondition('t.announcement_id', $announcement_id);
			$criteria->group = 'announcement_id';
		}
		$data = $this->model()->findAll($criteria);
		foreach ($data as $eachData) {
			$result[$eachData->announcement_id]["count_view"] = $eachData["count_view"];
		}
		return $result;
	}

	public function viewAnnouncement($announcement_id)
	{
		$result = array();
		// Count from assign
		$criteria = new CDbCriteria;
		$criteria->with = array('announcement', 'member', 'member.memberCode');
		if (!is_array($announcement_id) && $announcement_id != "") {
			$criteria->condition = 't.announcement_id = :announcement_id';
			$criteria->params = array(':announcement_id'=> $announcement_id);
		}
		elseif (is_array($announcement_id) && !empty($announcement_id)) {
			$criteria->addInCondition('t.announcement_id', $announcement_id);
		}
		//$criteria->addCondition("announcement.");
		$criteria->order = 'course.id ASC, member.fullname ASC';
		$data = $this->model()->findAll($criteria);
		foreach ($data as $eachData) {
			$result[$eachData->announcement_id][] = array("fullname"	=> $eachData->member->fullname,
														  "coursename" 	=> $eachData->member->memberCode->course->name);
		}
		return $result;
	}
}